<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ModelController extends Controller
{
    public function index(){
    	// $list=DB::table('model')->get();
    	$page = DB::table('model')->paginate(6);
		return view('admin.model.index',['page'=>$page]);
    }
    public function add(Request $request){
    	if($request->isMethod('get')){
			return  view('admin.model.add');
		}else{
			// 插入数据 返回插入数据的bool值
			$res = DB::table('model')->insert($request->except ('_token'));
			// 插入数据 返回插入数据的id
			// $bool = DB::table('model')->insertGetId($request->input());
			if($res){
				return redirect('model/index');
			}else{
				echo '输入有误，后期优化用户体验！';
			}
		}
    }
    public function edit(Request $request,$id=null){
    	if($request->isMethod('get')){
    		if($id){
    			$res=DB::table('model')->where('model_id', $id)->get();
				return  view('admin.model.edit',['res'=>$res[0]]);
    		}else{
				return  abort('404');
    		}
		}else{
			// 插入数据 返回插入数据的bool值
			$res = DB::table('model')->where('model_id',$request->input('id'))->update($request->except (['_token','id']));;
			if($res){
				return redirect('model/index');
			}else{
				echo '输入有误，后期优化用户体验！';
			}
		}
    }
    public function del(Request $request){
    	if($request->input('id')){
			$res =DB::table('model')->where('model_id',$request->input('id'))->delete();
			if($res){
				return 1;
			}else{
				return 2;
			}
    	}else{
			return  abort('404');
    	}
    }
}
